package com.example.xrecyclerview;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

/**
 * @author wang
 * @version 创建时间：2015年7月8日 上午11:31:11 类说明
 */
public abstract class BaseListActivity extends Activity {
    public XRecyclerView mRecyclerView;
    public BaseAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recyclerview);

        initXRecyclerView();

    }


    /**
     * 设置适配器
     *
     * @return
     */
    protected abstract BaseAdapter bindAdapter();

    /**
     * 初始化刷新和加载
     *
     * @return
     */
    protected abstract void onListRefresh();

    protected abstract void onListLoadMore();


    private void initXRecyclerView() {
        mRecyclerView = (XRecyclerView) this.findViewById(R.id.recyclerview);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        mRecyclerView.setLoadingMoreProgressStyle(ProgressStyle.BallRotate);
        mRecyclerView.setArrowImageView(R.drawable.iconfont_downgrey);


        mAdapter = bindAdapter();
        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.setItemNumOfPage(10);
        mRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {

                onListRefresh();
            }

            @Override
            public void onLoadMore() {
                onListLoadMore();
            }
        });


        View mEmptyView = LayoutInflater.from(this).inflate(R.layout.include_nodata, null);
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
        lp.addRule(RelativeLayout.CENTER_HORIZONTAL);
        lp.addRule(RelativeLayout.CENTER_VERTICAL);
        mEmptyView.findViewById(R.id.btn_empty_data).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mRecyclerView.refresh();
            }
        });

        ViewGroup parent = (ViewGroup) mRecyclerView.getParent();
        parent.addView(mEmptyView, lp);
        mRecyclerView.setEmptyView(mEmptyView);


        mRecyclerView.refresh();
    }


}